<?php

namespace app\index\service;

use app\common\ConfigBase;
use app\common\model\ToolModel;
use app\index\model\SellModel;

class SellService
{
    /**
     *生成销项单
     * @author xiejunyu
     */
    static public function create($param)
    {
        if (empty($param['po_id'])) {
            return returnArr(500, '采购单id不存在!');
        }
        $sell = ToolModel::findByAttributes('purchase_sell', ['po_id' => $param['po_id']]);
        if (!empty($sell)) {
            return returnArr(500, '已存在销项单!');
        }
        if (empty($param['list'])) {
            return returnArr(500, '销项明细不能为空!');
        }
        $temp = [];
        $temp['po_id'] = $param['po_id'];
        $temp['bs_code'] = $param['bs_code']??'';
        $temp['money'] = $param['money'];
        $temp['supplier_company_id'] = $param['supplier_company_id'];
        $temp['purchase_company_id'] = $param['purchase_company_id'];
        $temp['ship_warehouse_id'] = $param['ship_warehouse_id']??0;
        $temp['user'] = $param['user'];
        $temp['ctime'] = $param['ctime'];
        $id = ToolModel::insertData($temp, 'purchase_sell');
        if(!$id){
            return returnArr(500, '生成销售单失败!');
        }
        ToolModel::updateData(['id' => $id], ['code' => 'XX' . $id], 'purchase_sell');
        $pd = [];
        foreach ($param['list'] as $v) {
            $tempPd = [];
            $tempPd['sell_id'] = $id;
            $tempPd['sku'] = $v['sku'];
            $tempPd['sku_name'] = $v['sku_name'];
            $tempPd['quantity'] = $v['quantity'];
            $tempPd['unit_price'] = $v['unit_price'];
            $tempPd['money'] = $v['money'];
            $tempPd['ctime'] = $v['ctime'];
            $pd[] = $tempPd;
        }
        $id=ToolModel::insertData($pd, 'purchase_sell_detail');
        if(!$id){
            return returnArr(500, '生成销售单明细失败!');
        }
        return returnArr(1, '成功');
    }
    static public function detail($param){
        $result=ToolModel::select('purchase_sell',['id'=>$param['id']]);
        $detail=ToolModel::select('purchase_sell_detail',['sell_id'=>$param['id']]);
        $result=self::getListHandle($result);
        return returnArr(1,'',['result'=>$result[0],'detail'=>$detail]);
    }
    static public function getList($param){
        if(empty($param['down'])){
            $titles=[];
        }else{
            $titles=[
                'code'=>'销项单号',
                'supplier_company_str'=>'销售公司',
                'ship_warehouse_str'=>'发货仓库',
                'purchase_company_str'=>'客户名称',
                'purchase_addr'=>'客户地址',
                'sku_num'=>'sku',
                'money'=>'总金额',
                'ctime'=>'销售时间'
            ];
        }
        return SellModel::getList($param,$titles,"\app\index\service\SellService::getListHandle");
    }
    static public function getListHandle($data){
        $company=ConfigBase::getCompanyAll();
        $warehouse=ConfigBase::getWarehouseAll();
        $skuNum=SellModel::getSKuNum(array_column($data,'id'));
        $companyAddr=ToolModel::select('base_company',['id'=>['in',array_column($data,'purchase_company_id')]],'address','','id');
        foreach ($data as &$v){
            $v['supplier_company_str']=$company[$v['supplier_company_id']];
            $v['purchase_company_str']=$company[$v['purchase_company_id']];
            $v['ship_warehouse_str']=$warehouse[$v['ship_warehouse_id']]??'';
            $v['sku_num']=$skuNum[$v['id']];
            $v['purchase_addr']=$companyAddr[$v['purchase_company_id']];
        }
        return $data;
    }
}